<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

$_current_dir = dirname(__FILE__) . '/';
require($_current_dir . 'includes/application_top.php');
//
include(CONFIG_PATH . 'lib/xajax_0.2.4/xajax.inc.php');
//
$action = trim(strtolower(util_get_param($_GET, 'action')));
$page = trim(strtolower(util_get_param($_GET, 'page')));
if( !is_numeric($page) ) {
	$page = 1;
}

///////////////////////////////////////////////////////////////////////////////
//
function _alert_auth() {
	$objResponse = new xajaxResponse();
	$msg = "You dont have permission!";
	$objResponse->addAlert($msg);
	
	return $objResponse;
}

function _alert_message($msg) {
	$objResponse = new xajaxResponse();
	$objResponse->addAlert($msg);
	
	return $objResponse;
}

function _script_cb($call, $result) {
	$objResponse = new xajaxResponse();
	$objResponse->addScriptCall($call, $result);
	
	return $objResponse;
}

function _check_auth($type = 'admin') {
	if( $type == 'admin' ) {
		if( $_SESSION['suser']->userid > 0 && $_SESSION['suser']->usertype == 'admin' ) {
			return true;
		}
	} elseif( $type == 'member' ) {
		if( $_SESSION['suser']->userid > 0 && in_array($_SESSION['suser']->usertype, array('member', 'admin')) ) {
			return true;
		}
	} else {
		if( $_SESSION['suser']->userid == 0 ) {
			return true;
		}
	}
	
	return false;
}

///////////////////////////////////////////////////////////////////////////////
//ADMINS
///////////////////////////////////////////////////////////////////////////////
function wsa_deleteimage($id, $type = '') {
	if( _check_auth() == false ) {
		return _alert_auth();
	}
	
	//
	if( $type == 'gallery' ) {
		include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/gallery/gallery.image.class.php');
		$mObj = new GalleryImageBase();
		
		$mObj->deleteItem($id);
		$msg = 'OK';
	} elseif( $type == 'product' || $type == 'p' ) {
		include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/product/product.image.class.php');
		$mObj = new ProductImageBase();
		
		$mObj->deleteItem($id);
		$msg = 'OK';
	} elseif( $type == 'i' ) {
		include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/common/image.class.php');
		$mObj = new CommonImageBase();
		
		$mObj->deleteItem($id);
		$msg = 'OK';
	} else {
		$msg = 'ERROR';
	}
	//
	$objResponse = new xajaxResponse();
	$objResponse->addAlert($msg);
	$objResponse->addAssign("dimg_".$id, "innerHTML", "");
	
	return $objResponse;
}

///////////////////////////////////////////////////////////////////////////////
// Instantiate the xajax object.  No parameters defaults requestURI to this page, method to POST, and debug to off
$xajax = new xajax();

//$xajax->debugOn(); // Uncomment this line to turn debugging on

// Specify the PHP functions to wrap. The JavaScript wrappers will be named xajax_functionname
//
//
$xajax->registerFunction("wsa_deleteimage");

// Process any requests.  Because our requestURI is the same as our html page,
// this must be called before any headers or HTML output have been sent
$xajax->processRequests();
//
?>